const db = require("../db/index");
// 导入加密中间件
const bcrypt = require("bcryptjs");
// 导入jwt
const jwt = require("../utils/JWT");

// 用户管理员路由
const userAdminRoutes = [
  "user",
  "adminProduct",
  "adminUser",
  "message",
  "userList",
];
// 产品管理员路由
const productAdminRoutes = ["product", "productList", "outList"];
// 消息管理员路由
const messageAdminRoutes = ["adminMessage", "messageList", "recycle"];
// 用户路由
const userRoutes = ["product", "productList", "outList"];
// 超级管理员
const superAdminRoutes = [
  "user",
  "adminProduct",
  "adminUser",
  "message",
  "userList",
  "product",
  "productList",
  "outList",
  "adminMessage",
  "messageList",
  "recycle",
];
const loginController = {
  register: async (req, res) => {
    const regInfo = req.body;
    // 判断有无账号密码
    if (!regInfo.account || res.password) {
      res.send({
        code: -1,
        message: "账号或密码不能为空",
      });
    }
    // 判断账号是否已存在
    const sql = "SELECT * FROM users WHERE account = ?";
    // db.query(sql, regInfo.account, (err, result) => {
    const [data] = await db.query(sql, regInfo.account);
    if (data.length > 0) {
      return res.send({
        code: -1,
        message: "账号已存在",
      });
    }
    // 加密密码
    regInfo.password = bcrypt.hashSync(regInfo.password, 10);
    // 账号密码加入数据库
    const sql1 = "INSERT INTO users SET ?";
    // 注册身份
    const identity = "用户";
    // 创建时间
    const create_time = new Date();
    // 插入数据表
    try {
      await db.query(sql1, {
        // 用户名
        account: regInfo.account,
        // 密码
        password: regInfo.password,
        // 身份
        identity,
        // 创建时间
        create_time,
        // 是否冻结  1 - 未冻结
        status: 1,
      });
    } catch (err) {
      return res.send({
        status: -1,
        message: "注册失败，请稍后再试",
      });
    }
    res.send({
      code: 1,
      message: "注册成功",
    });
    // });
  },
  login: async (req, res) => {
    const loginInfo = req.body;
    // 判断数据库中有无账号
    const sql = "SELECT * FROM users WHERE account = ?";
    try {
      //
      const [data] = await db.query(sql, loginInfo.account);
      if (data.length !== 1) {
        return res.cc("未找到该账号，请先注册");
      }
      // 密码解密
      const compareResult = bcrypt.compareSync(
        loginInfo.password,
        data[0].password
      );
      if (!compareResult) {
        return res.cc("账号或密码错误");
      }
      // 判断账号是否冻结
      if (data[0].status !== 1) {
        return res.cc("账号被冻结");
      }
      // 生成token
      const token = jwt.generate({ username: loginInfo.account }, "18h");
      res.send({
        data: data[0].id,
        status: 1,
        message: "登录成功",
        token: "Bearer " + token,
      });
    } catch (err) {
      return res.cc(err);
    }
  },
  getRoutes: async (req, res) => {
    const { id } = req.body;
    const sql = "SELECT identity FROM users WHERE id = ?";
    try {
      const [data] = await db.query(sql, id);
      let menu = [];
      if (data[0].identity === "超级管理员") {
        menu = superAdminRoutes;
      } else if (data[0].identity === "用户管理员") {
        menu = userAdminRoutes;
      } else if (data[0].identity === "产品管理员") {
        menu = productAdminRoutes;
      } else if (data[0].identity === "消息管理员") {
        menu = messageAdminRoutes;
      } else {
        menu = userRoutes;
      }
      res.send({
        status: 1,
        message: "成功",
        data: menu,
      });
    } catch (err) {
      res.cc(err);
    }
  },
};

module.exports = loginController;
